const { extend, Toast } = require('../../zanui/index')
import { HOST, UPLOAD_HOST } from '../../config/apiConfig.js'
import { obj2Form } from '../../utils/util.js'

const app = getApp()

Page(extend({}, Toast, {
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    payTypeIconUrl: '../../assets/img/add_img.png',
    payTypeName: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id
    wx.setNavigationBarTitle({
      title: '添加支付方式'
    })
    if (id != undefined) {
      wx.setNavigationBarTitle({
        title: '编辑支付方式'
      })
      wx.showLoading({
        title: '加载中...',
      })
      const _self = this
      wx.request({
        url: HOST + '/v1/payType?id=' + id,
        method: 'GET',
        success: (res) => {
          wx.hideLoading()
          const data = res.data.data[0]
          _self.setData({
            id: data.id,
            payTypeIconUrl: data.payTypeIconUrl,
            payTypeName: data.payTypeName
          })
        },
        fail: () => {

        },
        complete: () => {

        }
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  selectImgs(e) {
    const _self = this
    wx.chooseImage({
      count: 12,
      sizeType: ['original'],
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        })
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: UPLOAD_HOST,
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            wx.hideLoading()
            let data = JSON.parse(res.data)
            _self.setData({
              payTypeIconUrl: data.data[0].downloadPath
            })
          }
        })
      }
    })
  },
  // 表单提交
  formSubmit: function (event) {
    const _self = this
    // 最后保存的数据
    const data = {}
    let payTypeName = event.detail.value.payTypeName.trim()
    if (payTypeName == '') {
      _self.showZanToast('支付方式不能为空')
      return
    }
    data.payTypeName = payTypeName
    // 如果图片为本地图片,则不保存
    if (this.data.payTypeIconUrl != '../../assets/img/add_img.png' && this.data.payTypeIconUrl != undefined) {
      data.payTypeIconUrl = this.data.payTypeIconUrl
    }else{
      data.payTypeIconUrl = ''
    }
    wx.showLoading({
      title: '保存中...',
    })
    // 如果id不存在则添加数据,否则更新数据
    if (this.data.id == '') {
      data.createBy = app.globalData.userInfo.openid
      wx.request({
        url: HOST + '/v1/payType',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: obj2Form(data),
        method: 'POST',
        success: function (res) {
          if (res.statusCode != 200) {
            _self.showZanToast('请求接口失败')
            return
          }
          if (res.data.code != '000000') {
            _self.showZanToast(res.data.message)
            return
          }
          wx.navigateBack({

          })
        },
        fail: function () {
          _self.showZanToast('网络错误')
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    } else {
      data.updateBy = app.globalData.userInfo.openid
      data.id = this.data.id
      wx.request({
        url: HOST + '/v1/payType/' + data.id,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: obj2Form(data),
        method: 'PUT',
        success: function (res) {
          if (res.statusCode != 200) {
            _self.showZanToast('请求接口失败')
            return
          }
          if (res.data.code != '000000') {
            _self.showZanToast(res.data.message)
            return
          }
          // 返回上一页
          wx.navigateBack({

          })
        },
        fail: function () {
          _self.showZanToast('网络错误')
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    }
  }
}))
